package com.docv.web.events;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.model.Layout;
import com.liferay.portal.model.PortletPreferences;
import com.liferay.portal.service.LayoutLocalServiceUtil;
import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.PortletPreferencesFactoryUtil;
import com.liferay.portlet.layoutconfiguration.util.RuntimePortletUtil;

public class PorletHelper {

	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public PorletHelper(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}
	
	private void setPortletPreferences(String portletId, String confgPage) throws Exception {
		if(!confgPage.startsWith(StringPool.SLASH)) confgPage = StringPool.SLASH + confgPage;
		ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
		try {
			Layout confgLayout = LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), false, confgPage);
			long confgPlid = confgLayout.getPlid();
			long plid = themeDisplay.getPlid();
			PortletPreferences configPref = PortletPreferencesLocalServiceUtil.getPortletPreferences(0, 3, confgPlid, portletId);
			PortletPreferences pref = null;
			try {
				pref = PortletPreferencesLocalServiceUtil.getPortletPreferences(0, 3, plid, portletId);
			} catch (Exception e) {
			}
			if(pref == null) {
				PortletPreferencesFactoryUtil.getPortletSetup(request, portletId, configPref.getPreferences());
			} else {
				String configPrefStr = configPref.getPreferences();
				String prefStr = pref.getPreferences();
				if(configPrefStr.length() != prefStr.length() || !configPrefStr.equals(prefStr)) {
					pref.setPreferences(configPref.getPreferences());
					PortletPreferencesLocalServiceUtil.updatePortletPreferences(pref);
				}
			}
		} catch (Exception e) {
			if(e != null) _log.info(e.getMessage());
		}
	}
	
	public String processPortlet(String portletId, String confgPage) throws Exception {
		return processPortlet(portletId, confgPage, StringPool.BLANK);
	}
	
	public String processPortlet(String portletId, String confgPage, String queryString) throws Exception {
		setPortletPreferences(portletId, confgPage);
		try {
			request.setAttribute(WebKeys.RENDER_PORTLET_RESOURCE, Boolean.TRUE);
			return RuntimePortletUtil.processPortlet(
					request.getServletContext(), request, response, null, null, portletId, queryString, false);
		} finally {
			request.removeAttribute(WebKeys.RENDER_PORTLET_RESOURCE);
		}
	}
	
	private static Log _log = LogFactoryUtil.getLog(PorletHelper.class);
}
